package service

import (
	"github.com/gin-gonic/gin"
	"monment/basic/config"
	"monment/handler/model"
	"monment/handler/request"
	"net/http"
)

func AddComment(c *gin.Context) {
	var form request.AddComment
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	comment := model.Comment{
		UserId:  form.UserId,
		PostId:  form.PostId,
		Content: form.Content,
	}
	if err := comment.AddComment(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "评论失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "评论成功",
	})
	return
}
